Function Set-FTPConnection
{ 

	[CmdletBinding(
    	SupportsShouldProcess=$True,
        ConfirmImpact="Low"
    )]
    Param(
		#[parameter(Mandatory=$true)]
		[Alias("Credential")]
		$Credentials,
		[parameter(Mandatory=$true)]
		[String]$Server,
        [String]$User,
        [String]$Pass,
		[Switch]$EnableSsl = $False,
		[Switch]$ignoreCert = $False,
		[Switch]$KeepAlive = $False,
		[Switch]$UseBinary = $False,
		[Switch]$UsePassive = $true,
		[String]$Session = "DefaultFTPSession"
	)
	
	Begin
	{
		if($Credentials -isnot [System.Management.Automation.PSCredential])
		{
            $Credentials= New-Object System.Net.NetworkCredential($User,$Pass)
			#$Credentials = Get-Credential $Credentials
		}
	}
	
	Process
	{
        if ($pscmdlet.ShouldProcess($Server,"Connect to FTP Server")) 
		{	
			if(!($Server -match "ftp://"))
			{
				$Server = "ftp://"+$Server	
				Write-Debug "Add ftp:// at start: $Server"				
			}
			
			Write-Verbose "Create FtpWebRequest object."
			[System.Net.FtpWebRequest]$Request = [System.Net.WebRequest]::Create($Server)
			$Request.Credentials = $Credentials#New-Object System.Net.NetworkCredential($User,$Pass),
			$Request.EnableSsl = $EnableSsl
			$Request.KeepAlive = $KeepAlive
			$Request.UseBinary = $UseBinary
			$Request.UsePassive = $UsePassive
			$Request | Add-Member -MemberType NoteProperty -Name ignoreCert -Value $ignoreCert
			$Request | Add-Member -MemberType NoteProperty -Name Session -Value $Session

			$Request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
			Try
			{
				[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$ignoreCert}
				$Response = $Request.GetResponse()
				$Response.Close()
				
				if((Get-Variable -Scope Global -Name $Session -ErrorAction SilentlyContinue) -eq $null)
				{
					Write-Verbose "Create global variable: $Session"
					New-Variable -Scope Global -Name $Session -Value $Request
				}
				else
				{
					Write-Verbose "Set global variable: $Session"
					Set-Variable -Scope Global -Name $Session -Value $Request
				}
				
				Return $Response
			}
			Catch
			{
				Write-Error $_.Exception.Message -ErrorAction Stop 
			}
		}
	}
	
	End{}				
}